import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

import '../models/image_normal_model.dart';
import 'utils_index.dart';

class ImagePickerConfig {
  ImagePicker? imagePicker;
  bool isMult = false;
  double? maxWidth;
  double? maxHeight;
  int? imageQuality;
  CameraDevice preferredCameraDevice = CameraDevice.rear;
  bool requestFullMetadata = true;
}

class ImagePickerTool {
  /*
   * 配置相册相机功能 系统风格，支持全部平台
   * */
  static configImagePickerTool({
    required BuildContext context,
    ImagePicker? imagePicker,
    bool isMult = false,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    bool requestFullMetadata = true,
    bool isShowPop = true,
    int type = 0,
  }) async {
    List<XFile>? selectImage;
    if (type == 0) {
      selectImage = await configGallery(
        imagePicker: imagePicker,
        isMult: isMult,
        maxHeight: maxHeight,
        maxWidth: maxWidth,
        imageQuality: imageQuality,
        preferredCameraDevice: preferredCameraDevice,
        requestFullMetadata: requestFullMetadata,
      );
    } else {
      selectImage = await configCamera(
        imagePicker: imagePicker,
        maxHeight: maxHeight,
        maxWidth: maxWidth,
        preferredCameraDevice: preferredCameraDevice,
        requestFullMetadata: requestFullMetadata,
      );
    }
    return selectImage;
  }

  /// 相册
  static configGallery({
    ImagePicker? imagePicker,
    bool isMult = false,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    bool requestFullMetadata = true,
  }) async {
    imagePicker ??= ImagePicker();
    List<XFile>? selectImage;
    if (isMult) {
      selectImage = await imagePicker.pickMultiImage(
        maxHeight: maxHeight,
        maxWidth: maxWidth,
        imageQuality: imageQuality,
        requestFullMetadata: requestFullMetadata,
      );
    } else {
      XFile? image = await imagePicker.pickImage(
        source: ImageSource.gallery,
        maxHeight: maxHeight,
        maxWidth: maxWidth,
        imageQuality: imageQuality,
        preferredCameraDevice: preferredCameraDevice,
        requestFullMetadata: requestFullMetadata,
      );
      if (image != null) {
        selectImage = [image];
      }
    }
    return selectImage;
  }

  /// 相机
  static configCamera({
    ImagePicker? imagePicker,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    bool requestFullMetadata = true,
  }) async {
    imagePicker ??= ImagePicker();
    List<XFile>? selectImage;
    XFile? image = await imagePicker.pickImage(
      source: ImageSource.camera,
      maxHeight: maxHeight,
      maxWidth: maxWidth,
      imageQuality: imageQuality,
      preferredCameraDevice: preferredCameraDevice,
      requestFullMetadata: requestFullMetadata,
    );
    if (image != null) {
      selectImage = [image];
    }
    return selectImage;
  }

  /// 把图片转换成XFile
  static Future<List<XFile>> configImageConversion({
    List<ImageNormalModel>? list,
  }) async {
    List<XFile> xList = [];
    if (list != null) {
      for (var i = 0; i < list.length; i++) {
        ImageNormalModel imageNormalModel = list[i];
        Uint8List? byte = await configImageDataWithBytes(
          imageNormalModel: imageNormalModel,
        );
        if (byte != null) {
          String name;
          if (imageNormalModel.byte != null || imageNormalModel.file != null) {
            name = "hzy_shar_image_$i";
          } else {
            name = "hzy_share_image_${imageNormalModel.url}";
          }
          XFile xFile = XFile.fromData(
            byte,
            name: name,
            mimeType: 'image/png',
          );
          xList.add(xFile);
        }
      }
    }
    return xList;
  }
}
